package com.smartalarmclock.UI.Games;

import com.smartalarmclock.UI.R;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainGamesActivity extends Activity {
	
	private RadioGroup radioGameGroup;
	private RadioButton radioGameButton;
	private Button buttonStartGame;
	private MediaPlayer m_MediaPlayer = new MediaPlayer();
	
	private boolean gameWon = false;

	@Override
	public void onBackPressed() {
		if (gameWon)
		{
			super.onBackPressed();
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.games_main);
		
		try {
			Uri alert =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
			m_MediaPlayer.setDataSource(this, alert);
			final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0)
			{
				m_MediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
				m_MediaPlayer.setLooping(true);
				m_MediaPlayer.prepare();
				m_MediaPlayer.start();
				
			}
			
		} catch(Exception e) {
			
		}   
		/*
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
        */
		addListenerOnButton();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	  super.onActivityResult(requestCode, resultCode, data);
	  m_MediaPlayer.start();
	  if (resultCode == Activity.RESULT_OK)
	  {
		  gameWon = true;
		  m_MediaPlayer.stop();
		  onBackPressed();
	  }
	}
	
	public void addListenerOnButton() {
		
		radioGameGroup = (RadioGroup) findViewById(R.id.radioGame);
		buttonStartGame = (Button) findViewById(R.id.buttonStart);
		
		buttonStartGame.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {	
				m_MediaPlayer.stop();
				String userChoice;
		        // get selected radio button from radioGroup
				int selectedId = radioGameGroup.getCheckedRadioButtonId();
				
				// find the radiobutton by returned id
		        radioGameButton = (RadioButton) findViewById(selectedId);
		        
		        userChoice = radioGameButton.getText().toString();
		        
        		if("Math Exercise".equals(userChoice))
        		{
        			Intent vipActivity = new Intent(MainGamesActivity.this, MathExerciseActivity.class);
        			startActivityForResult(vipActivity, 0);
        		}
        		else if("Text Input".equals(userChoice))
        		{
        			Intent groupActivity = new Intent(MainGamesActivity.this,TextInputActivity.class);     
        			startActivityForResult(groupActivity, 1);
        		}
			}
			});
		}
}
